﻿using System;
using System.Collections.Generic;
using System.Text;
using CLoggerNamespace;

namespace HL7v_2x
{
    public class HL7SegmentNK1 : IHL7Segment
    {
 #region DESARMAR SEGMENTO EN CAMPOS
        public List<HL7Fields> DesarmarSegmentoEnCampos(string strSegment)
          {
             List<HL7Fields> lstHL7Fields = new List<HL7Fields>();
              //EJEMPLO MENSAJE HL7
              /*
                  
              */

              string[] strArrAux = null;

              try
              {
                  strArrAux = strSegment.Split('|');
                  int inCantBarras = strArrAux.Length - 1;
                  CLogger.Append("Cantidad de Barras: " + inCantBarras + " - MAXIMO DE 37 CAMPOS.", CLogger.INFO);
              }
              catch(Exception ex)
              {

                  throw new Exception("ERROR PARCEANDO LAS BARRAS | Ex: " + ex.Message);
              }
              try
              {

                  if ((strArrAux.Length > 0) && (strArrAux[0] != null) && (strArrAux[0] != ""))    // FieldSeparator_MSH = 00001,  
                  {
                      CLogger.Append("Field 0: " + strArrAux[0], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.SetID_NK1, enc.GetBytes(strArrAux[0])));
                  }
                  if ((strArrAux.Length > 1) && (strArrAux[1] != null) && (strArrAux[1] != null))  // EncodingCharacters_MSH = 00002,
                  {
                      CLogger.Append("Field 1: " + strArrAux[1], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.Name_NK1, enc.GetBytes(strArrAux[1])));
                  }
                  if ((strArrAux.Length > 2) && (strArrAux[2] != "") && (strArrAux[2] != null)) // SendingApplication_MSH = 00003,
                  {
                      CLogger.Append("Field 2: " + strArrAux[2], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.Relationship_NK1, enc.GetBytes(strArrAux[2])));
                  }
                  if ((strArrAux.Length > 3) && (strArrAux[3] != "") && (strArrAux[3] != null)) // SendingFacility_MSH = 00004,
                  {
                      CLogger.Append("Field 3: " + strArrAux[3], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.Address_NK1, enc.GetBytes(strArrAux[3])));
                  }
                  if ((strArrAux.Length > 4) && (strArrAux[4] != "") && (strArrAux[4] != null)) // ReceivingFacility_MSH = 00005,
                  {
                      CLogger.Append("Field 4: " + strArrAux[4], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.PhoneNumber_NK1, enc.GetBytes(strArrAux[4])));
                  }
                  if ((strArrAux.Length > 5) && (strArrAux[5] != "") && (strArrAux[5] != null)) // ReceivingApplication_MSH = 00006,
                  {
                      CLogger.Append("Field 5: " + strArrAux[5], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.BusinessPhoneNumber_NK1, enc.GetBytes(strArrAux[5])));
                  }
                  if ((strArrAux.Length > 6) && (strArrAux[6] != "") && (strArrAux[6] != null)) // DateTimeofMessage_MSH = 00007,
                  {
                      CLogger.Append("Field 6: " + strArrAux[6], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.ContactRole_NK1, enc.GetBytes(strArrAux[6])));
                  }
                  if ((strArrAux.Length > 7) && (strArrAux[7] != "") && (strArrAux[7] != null)) // Security_MSH = 00008,
                  {
                      CLogger.Append("Field 7: " + strArrAux[7], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.StartDate_NK1, enc.GetBytes(strArrAux[7])));
                  }
                  if ((strArrAux.Length > 8) && (strArrAux[8] != "") && (strArrAux[8] != null)) // MessageType_MSH = 00009,
                  {
                      CLogger.Append("Field 8: " + strArrAux[8], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.EndDate_NK1, enc.GetBytes(strArrAux[8])));
                  }
                  if ((strArrAux.Length > 9) && (strArrAux[9] != "") && (strArrAux[9] != null))// MessageControlID_MSH = 00010,
                  {
                      CLogger.Append("Field 9: " + strArrAux[9], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.NextofKinAssociatedPartiesJobTitle_NK1, enc.GetBytes(strArrAux[9])));
                  }
                  if ((strArrAux.Length > 10) && (strArrAux[10] != "") && (strArrAux[10] != null)) // ProcessingID_MSH = 00011,
                  {
                      CLogger.Append("Field 10: " + strArrAux[10], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.NextofKinAssociatedPartiesJobCodeClass_NK1, enc.GetBytes(strArrAux[10])));
                  }
                  if ((strArrAux.Length > 11) && (strArrAux[11] != "") && (strArrAux[11] != null)) // VersionID_MSH = 00012,
                  {
                      CLogger.Append("Field 11: " + strArrAux[11], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.NextofKinAssociatedPartiesEmployeeNumber_NK1, enc.GetBytes(strArrAux[11])));
                  }
                  if ((strArrAux.Length > 12) && (strArrAux[12] != "") && (strArrAux[12] != null)) // SequenceNumber_MSH = 00013,
                  {
                      CLogger.Append("Field 12: " + strArrAux[12], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.OrganizationName_NK1, enc.GetBytes(strArrAux[12])));
                  }
                  if ((strArrAux.Length > 13) && (strArrAux[13] != "") && (strArrAux[13] != null)) //ContinuationPointer_MSH = 00014,
                  {
                      CLogger.Append("Field 13: " + strArrAux[13], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.MaritalStatus_PID_NK1, enc.GetBytes(strArrAux[13])));
                  }
                  if ((strArrAux.Length > 14) && (strArrAux[14] != "") && (strArrAux[14] != null)) //  AcceptAknowledgmentType_MSH = 00015,
                  {
                      CLogger.Append("Field 14: " + strArrAux[14], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.AdministrativeSex_PID_NK1, enc.GetBytes(strArrAux[14])));
                  }
                  if ((strArrAux.Length > 15) && (strArrAux[15] != "") && (strArrAux[15] != null)) // ApplicationAknowledgmentType_MSH = 00016,
                  {
                      CLogger.Append("Field 15: " + strArrAux[15], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();                      
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.DateTimeofBirth_PID_NK1, enc.GetBytes(strArrAux[15])));                      
                  }                 

                  if ((strArrAux.Length > 16) && (strArrAux[16] != null) && (strArrAux[16] != "")) //CountryCode_MSH=00017,
                  {
                      CLogger.Append("Field 16: " + strArrAux[16], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.LivingDependency_NK1, enc.GetBytes(strArrAux[16])));
                  }
                  if ((strArrAux.Length > 17)&&(strArrAux[17] != null) && (strArrAux[17] != "")) // CharacterSet_MSH=00692,
                  {
                      CLogger.Append("Field 17: " + strArrAux[17], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.AmbulatoryStatus_PV1_NK1, enc.GetBytes(strArrAux[17])));
                  }
                  if ((strArrAux.Length > 18) && (strArrAux[18] != null) && (strArrAux[18] != "")) // PrincipalLanguageofMessage_MSH=00693,
                  {
                      CLogger.Append("Field 18: " + strArrAux[18], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.Citizenship_PID_NK1, enc.GetBytes(strArrAux[18])));
                  }
                  if ((strArrAux.Length > 19) && (strArrAux[19] != null) && (strArrAux[19] != "")) // AlternateCharacterSetHandlingScheme_MSH=01317,
                  {
                      CLogger.Append("Field 19: " + strArrAux[19], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.PrimaryLanguage_PID_NK1, enc.GetBytes(strArrAux[19])));
                  }
                  if ((strArrAux.Length > 20 ) && (strArrAux[20] != null) && (strArrAux[20] != "")) //MessageProfileIdentifier_MSH=01598,
                  {
                      CLogger.Append("Field 20: " + strArrAux[20], CLogger.INFO);
                      System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                      lstHL7Fields.Add(new HL7Fields(HL7FieldName.LivingArrangement_NK1, enc.GetBytes(strArrAux[20])));
                  }

                  if ((strArrAux.Length > 21) && (strArrAux[21] != "") && (strArrAux[21] != null)) //   CourtesyCode_PV1 = 00152,
                {
                    CLogger.Append("Field 21: " + strArrAux[21], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.PublicityCode_NK1, enc.GetBytes(strArrAux[21])));
                }
                if ((strArrAux.Length > 22) && (strArrAux[22] != "") && (strArrAux[22] != null)) //   CreditRating_PV1 = 00153,
                {
                    CLogger.Append("Field 22: " + strArrAux[22], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ProtectionIndicator_NK1, enc.GetBytes(strArrAux[22])));
                }

                if ((strArrAux.Length > 23) && (strArrAux[23] != "") && (strArrAux[23] != null)) //   ContractCode_PV1 = 00154,
                {
                    CLogger.Append("Field 23: " + strArrAux[23], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.StudentIndicator_NK1, enc.GetBytes(strArrAux[23])));
                }
                if ((strArrAux.Length > 24) && (strArrAux[24] != "") && (strArrAux[24] != null)) //   ContractEffectiveDate_PV1 = 00155,
                {
                    CLogger.Append("Field 24: " + strArrAux[24], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.Religion_PID_NK1, enc.GetBytes(strArrAux[24])));
                }
                if ((strArrAux.Length > 25) && (strArrAux[25] != "") && (strArrAux[25] != null)) //  ContractAmount_PV1 = 00156,
                {
                    CLogger.Append("Field 25: " + strArrAux[25], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.MotherMaidenName_PID_NK1, enc.GetBytes(strArrAux[25])));
                }
                if ((strArrAux.Length > 26) && (strArrAux[26] != "") && (strArrAux[26] != null)) //   ContractPeriod_PV1 = 00157,
                {
                    CLogger.Append("Field 26: " + strArrAux[26], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.Nationality_PID_NK1, enc.GetBytes(strArrAux[26])));
                }
                if ((strArrAux.Length > 27) && (strArrAux[27] != "") && (strArrAux[27] != null)) //   InterestCode_PV1 = 00158,
                {
                    CLogger.Append("Field 27: " + strArrAux[27], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.EthnicGroup_PID_NK1, enc.GetBytes(strArrAux[28])));
                }
                if ((strArrAux.Length > 28) && (strArrAux[28] != "") && (strArrAux[28] != null)) // TransfertoBadDebtCode_PV1 = 00159,
                {
                    CLogger.Append("Field 28: " + strArrAux[28], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ContactReason_NK1, enc.GetBytes(strArrAux[28])));
                }

                if ((strArrAux.Length > 29) && (strArrAux[29] != "") && (strArrAux[29] != null)) //TransfertoBadDebtDate_PV1 = 00160,
                {
                    CLogger.Append("Field 29: " + strArrAux[29], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ContactPersonName_NK1, enc.GetBytes(strArrAux[29])));
                }
                if ((strArrAux.Length > 30) && (strArrAux[30] != "") && (strArrAux[30] != null)) // BadDebtAgencyCode_PV1 = 00161,
                {
                    CLogger.Append("Field 30: " + strArrAux[30], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ContactPersonTelephoneNumber_NK1, enc.GetBytes(strArrAux[30])));
                }
                if ((strArrAux.Length > 31) && (strArrAux[31] != "") && (strArrAux[31] != null)) // BadDebtTransferAmount_PV1 = 00162,
                {
                    CLogger.Append("Field 31: " + strArrAux[31], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ContactPersonAddress_NK1, enc.GetBytes(strArrAux[31])));
                }
                if ((strArrAux.Length > 32) && (strArrAux[32] != "") && (strArrAux[32] != null)) // BadDebtRecoveryAmount_PV1 = 00163,
                {
                    CLogger.Append("Field 32: " + strArrAux[32], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.NextofKinAssociatedPartyIdentifiers_NK1, enc.GetBytes(strArrAux[32])));
                }
                if ((strArrAux.Length > 33) && (strArrAux[33] != "") && (strArrAux[33] != null)) //DeleteAccountIndicator_PV1 = 00164,
                {
                    CLogger.Append("Field 33: " + strArrAux[33], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.JobStatus_NK1, enc.GetBytes(strArrAux[30])));
                }
                if ((strArrAux.Length > 34) && (strArrAux[34] != "") && (strArrAux[34] != null)) // DeleteAccountDate_PV1=00165,
                {
                    CLogger.Append("Field 34: " + strArrAux[34], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.Race_PID_NK1, enc.GetBytes(strArrAux[34])));
                }
                if ((strArrAux.Length > 35) && (strArrAux[35] != "") && (strArrAux[35] != null)) // DischargeDisposition_PV1 = 00166,
                {
                    CLogger.Append("Field 35: " + strArrAux[35], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.Handicap_IN1_NK1, enc.GetBytes(strArrAux[35])));
                }
                   if ((strArrAux.Length > 36) && (strArrAux[36] != "") && (strArrAux[36] != null)) //     DischargedtoLocation_PV1 = 00167,
                {
                    CLogger.Append("Field 36: " + strArrAux[36], CLogger.INFO);
                    System.Text.ASCIIEncoding enc = new ASCIIEncoding();
                    lstHL7Fields.Add(new HL7Fields(HL7FieldName.ContactPersonSocialSecurityNumber_NK1, enc.GetBytes(strArrAux[36])));
                }
              }
              catch (Exception ex)
              {

                  throw new Exception("EXCEPCION AL DESCOMPONER SEGMENTO NK1 - Ex: " + ex.Message);
              }

            CLogger.Append("**********************FINAL DEL DESCOMPONER SEGMENTO NK1 OK*******************", CLogger.INFO);

              return lstHL7Fields;

          }
        #endregion

        //public HL7SegmentNK1()
        //    : base(HL7MessageName.MessageNK1)
        //{

        //}

        //public int BuildFields(byte[] byteInputStreamParam)
        //{
        //    throw new NotImplementedException();
        //}

        //public int BuildFields(byte[] byteInputStreamParam, int intOffset)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
